﻿using Common.AspNetCore;
using Common.Infrastructure;
using Common.RunModels.File;
using File.Api.Service;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace File.Api.Controllers
{
    [Route("api/Info")]
    public class InfoController : ApiController
    {
        private readonly FileService fileService; 

        public InfoController(IServiceProvider provider, FileService auth)
             : base(provider)
        {
            this.fileService = auth;
        }

        [HttpGet, Route("File")]
        public async Task<ApiActionResult<FileInfoModel>> FileInfo(string id)
        {
            return await SafeExecuteAsync<FileInfoModel>(async (messages) =>
            {
                if (string.IsNullOrEmpty(id))
                {
                    return null;
                }

                List<string> ids = new List<string>() { id };
                var infos = await this.fileService.GetFileInfoAsync(ids);
                return infos != null && infos.Any() ? infos[0] : null;
            });
        }

        [HttpGet, Route("FileGroup")]
        public async Task<ApiActionResult<FileGroupInfoModel>> FileGroup(string id)
        {
            return await SafeExecuteAsync<FileGroupInfoModel>(async (messages) =>
            {
                if (string.IsNullOrEmpty(id))
                {
                    return null;
                }

                List<string> ids = new List<string>() { id };
                var infos = await this.fileService.GetFileGroupInfoAsync(ids);
                return infos != null && infos.Any() ? infos[0] : null;
            });
        }
    }
}
